<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>todoList</title>
</head>
<body>
    <div id="app">
        <!-- 用户输入内容 -->
        <input type="text" v-model="ipt" @keyup.enter="handlerIpt">
        <!-- 渲染用户内容 -->
            <!-- 渲染未完成 arr-->
        <h2>未完成的事情</h2>
        <unfinishlist :list='list' @fmh="handle"></unfinishlist>
            <!-- 渲染已完成 arr1-->
        <h2>已完成的事情</h2>
        <finishlist :list='list' @fmh="handle"></finishlist>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
   <script>
       let unfinishlist = {
           props:['list'],
           template:`<div><div v-for="item in unfinishlist">
            <input type="checkbox" @click.prevent="handlerCheck(item)">
            {{item.contents}}
            <button @click="del(item)">删除</button>
        </div></div>`,
        computed: {
               unfinishlist() {
                   return this.list.filter(ele => ele.isChecked === false)
               }
            },
            methods:{
                 handlerCheck(item) {
                    this.list[item.id].isChecked = !item.isChecked;
                    this.$emit('fmh',this.list)
                },
                del(item) {
                    this.list.splice(item.id, 1);
                    //因为删除之后，id是不会动态的变化
                    //重新排序，给id动态赋值，为了后续删除
                    this.list.forEach((ele, index) => {
                        ele.id = index
                    });
                    this.$emit('fmh', this.list)
                }
            }
       }
       let finishlist = {
            props:['list'],
            template: `<div><div v-for="item in finishlist">
            <input type="checkbox" checked @click.prevent="handlerCheck(item)">
            {{item.contents}}
            <button @click="del(item)">删除</button>
        </div></div>`,
         computed: {
               finishlist() {
                   return this.list.filter(ele => ele.isChecked === true)
               }
       },
       methods:{
            handlerCheck(item) {
               this.list[item.id].isChecked = !item.isChecked
               this.$emit('fmh', this.list)
           },
           del(item) {
               this.list.splice(item.id, 1);
               //因为删除之后，id是不会动态的变化
               //重新排序，给id动态赋值，为了后续删除
               this.list.forEach((ele, index) => {
                   ele.id = index
               });
               this.$emit('fmh', this.list)
           }
       }
    }
       let vm = new Vue({
           el:'#app',
            data:{
                ipt:'',
                list:[
                    {
                        id:0,
                        contents:'吃饭',
                        isChecked:false
                    }
                ]   
            },
            methods:{
                handlerIpt(){
                    this.list.push({
                        id:this.list.length,
                        contents:this.ipt,
                        isChecked:false
                    })
                    this.ipt = ''
                },
                handle(data){
                    this.list = data
                }
            },
            components:{
                unfinishlist: unfinishlist,
                finishlist: finishlist
            }
        })
   </script>
</body>
</html>